﻿/*********************************************************************
* 对话框层的定义，适用于IE和FireFox
* 
* zyl 2008-10-30 
*     2009-12-28 加入对话框移动功能
*

**********************************************************************
用法：

页面只需要引用这个文件，就可以使用以下接口：
openDialog: 打开对话框
hideDialog: 关闭对话框
onDialogCallBack: 对话框的回调函数，关闭对话框时会调用


*********************************************************************/




/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 以下为实现(接口定义在最后)

//
// 调用这个方法来生成对话框的必要元素
//
createDlg();


// 对话框配置的，全局变量
var cfgObj = null;

// 事件变量
var isDragBegin = false;
var beginDragX = 0;
var beginDragY = 0;
var currentX = 0;
var currentY = 0;

//
// 对话框配置类的定义
//
var cfgClass = function(sUrl, sTitle, callBack, nDlgWidth, nDlgHeight)
{
    this.url = sUrl;
    this.title = sTitle;
    this.width = nDlgWidth;
    this.height = nDlgHeight;
    this.callBack = callBack;
}

//
//生成对话框html
//
function createDlg()
{
    // 遮罩层
    document.write('<div id="divDialogOuter"  style="filter:alpha(opacity=30);-moz-opacity:0.3;opacity:0.3;background-color:#000;width:100%;height:100%;z-index:1000;position: absolute;left:0;top:0;display:none;overflow: hidden;">\n');
    document.write('    <iframe width="100%" height="100%" frameborder="0" scroll="none"></iframe>\n');
    document.write('</div>\n');


    // 对话框层
    document.write('<div id="divDialogInner" style="border:solid 1px #709CD2;background:#A1C2E5;padding:0px;width:780px;z-index:1001; position: absolute; display:none;top:15%; left:25%;>\n');
    document.write('	<div style="padding:2px 2px 2px 2px;text-align:center;vertical-align:middle;">\n');

    document.write('<table style="width:100%; height:100%; border:2px solid #A1C2E5;">');
    document.write('<tr style="height: 20px; repeat-x scroll left top; color:#FFFFFF; font-weight:bold; vertical-alignment:baseline;">');

    // 标题
    document.write('    <td id="divdlgTitle" style="padding-left:0px; padding-right:0px; cursor: move;">');
    document.write('            <span style="float:right; cursor:pointer;" onclick="javascript:hideDialog();"><img src="images/close1.gif" /></span>\n');
    document.write('            <span style="float:left" id="dlgTitle"></span>\n');
    document.write('</td></tr><tr style="background:#FFFFFF;"><td>');

    // 内容
    document.write('            <iframe width="100%" height="100%" frameborder="0" id="iframeDlg" scroll="auto"></iframe>\n');
    document.write('</td></tr></table>');

    // END 
    document.write('	</div>\n');
    document.write('</div>\n');

    // 事件绑定
    var oTitle = document.getElementById("divdlgTitle");
    if(window.addEventListener)
    {
        oTitle.addEventListener("mousedown",    dlgTitle_mousedown, false);
    }
    else if(window.attachEvent)
    {
        oTitle.attachEvent("onmousedown",       dlgTitle_mousedown);
    }
}

function setDlgDefaultPosition() {
    var dlg = document.getElementById("divDialogInner");
    if (cfgObj && cfgObj.width) {
        currentX = parseInt((document.body.clientWidth - cfgObj.width) / 2);
    }
    else {
        currentX = parseInt(document.body.clientWidth * 0.25);
    }
    
    if (cfgObj && cfgObj.height) {
        currentY = parseInt((document.body.clientHeight - cfgObj.height) / 2);
    }
    else {
        currentY = parseInt(document.body.clientHeight * 0.15);
    }

    dlg.style.left = currentX + "px";
    dlg.style.top = currentY + "px";
}

function dlgTitle_mousedown(e)
{
    isDragBegin = true;
    if (!e) e = window.event;
    beginDragX = e.clientX;
    beginDragY = e.clientY;
    if (window.addEventListener) {
        document.addEventListener("mouseup", dlgTitle_mouseup, false);
        document.addEventListener("mousemove", dlgTitle_mousemove, false);
    }
    else if (window.attachEvent) {
        document.attachEvent("onmouseup", dlgTitle_mouseup);
        document.attachEvent("onmousemove", dlgTitle_mousemove);
    }
}

function dlgTitle_mouseup(e) {
    if (!isDragBegin) return;
    isDragBegin = false;
    if (!e) e = window.event;
    try {
        if (window.addEventListener) {
            document.removeEventListener("mouseup", dlgTitle_mouseup, false);
            document.removeEventListener("mousemove", dlgTitle_mousemove, false);
        }
        else if (window.attachEvent) {
            document.dettachEvent("onmouseup", dlgTitle_mouseup);
            document.dettachEvent("onmousemove", dlgTitle_mousemove);
        }
    }
    catch (e) { }
}

function dlgTitle_mousemove(e)
{
    if( ! isDragBegin) return;
    if (!e) e = window.event;
    
    // move
    var xoff = e.clientX - beginDragX;
    var yoff = e.clientY - beginDragY;
    //msgInfo("xoff: " + xoff + ", yoff: " + yoff);
    if (xoff == 0 && yoff == 0) return;
    
    var dlg = document.getElementById("divDialogInner");
    currentX += xoff;
    currentY += yoff;
    
    dlg.style.left = currentX + "px";
    dlg.style.top = currentY + "px";
    
    beginDragX = e.clientX;
    beginDragY = e.clientY;
}

//
// 将设置的对话框应用到对话框中
//
function applyCfg()
{
    $("#divDialogInner").width(cfgObj.width);
    $("#divDialogInner").height(cfgObj.height);
    $("#dlgTitle").html(cfgObj.title);
    $("#iframeDlg").attr("src", cfgObj.url);
    setDlgDefaultPosition();
}

// 显示
function showFloat()                    //根据屏幕的大小显示两个层
{
    var range = getRange();
    $('#divDialogOuter').width(range.width);
    $('#divDialogOuter').height(range.height);
    $('#divDialogOuter').show();
    $('#divDialogInner').show();
}


function getRange()                      //得到屏幕的大小
{
      var top     = document.body.scrollTop;
      var left    = document.body.scrollLeft;
      var height  = document.body.clientHeight;
      var width   = document.body.clientWidth;

      if (top==0 && left==0 && height==0 && width==0)
      {
        top     = document.documentElement.scrollTop;
        left    = document.documentElement.scrollLeft;
        height  = document.documentElement.clientHeight;
        width   = document.documentElement.clientWidth;
      }
      return  {top:top ,left:left ,height:height ,width:width } ;
}




///////////////////////////////////////////////////////////////////////////////////////////////////////

//
// 接口：打开对话框
//
document.openDialog = function(sUrl, sTitle, callBack, nDlgWidth, nDlgHeight)
{
    if(typeof(nDlgWidth) == "undefined")
        nDlgWidth = 500;
    if(typeof(nDlgHeight) == "undefined")
        nDlgHeight = 300;
    
    if(cfgObj == null)
    {
        cfgObj = new cfgClass(sUrl, sTitle, callBack, nDlgWidth, nDlgHeight);
    }
    else
    {
        cfgObj.url = sUrl;
        cfgObj.title = sTitle;
        cfgObj.width = nDlgWidth;
        cfgObj.height = nDlgHeight;
        cfgObj.callBack = callBack;
    }

    applyCfg();
    showFloat();
}

//
// 接口：关闭对话框
// 
document.hideDialog = function()
{
    $("#divDialogOuter").hide();
    $("#divDialogInner").hide();

    //if(cfgObj.callBack)
    //    cfgObj.callBack("0");
}


//
// 接口：定义页面回调事件
//
document.onDialogCallBack = function(type, key, value)
{
	document.hideDialog();
    
    // 其它操作
    if(cfgObj != null && cfgObj.callBack)
    {
        cfgObj.callBack(type, key, value);
    }
}



